package com.springboot.demo.service.impl;


import com.cx.item.common.exception.CommonException;
import com.cx.item.common.service.impl.BaseServiceImpl;
import com.springboot.demo.mapper.BannerMapper;
import com.springboot.demo.model.example.BannerExample;
import com.springboot.demo.model.model.Banner;
import com.springboot.demo.model.vo.BannerVo;
import com.springboot.demo.service.BannerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by heavin on 2018/6/21.
 */
@Service
public class BannerServiceImpl extends BaseServiceImpl<Banner, BannerExample, BannerMapper> implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;


    @Override
    public List<BannerVo> queryList() {
        BannerExample example = new BannerExample();
        BannerExample.Criteria criteria = example.createCriteria();
        criteria.andBannerStatusEqualTo(1);
        example.setOrderByClause("banner_sort asc");
        List<Banner> bannerList = bannerMapper.selectByExample(example);
        List<BannerVo> bannerVoList = new ArrayList<>();
        BannerVo bannerVo;
        for (Banner banner : bannerList) {
            bannerVo = new BannerVo();
            BeanUtils.copyProperties(banner, bannerVo);
            bannerVoList.add(bannerVo);
        }

        return bannerVoList;
    }


    @Override
    public BannerVo queryById(Integer id) {
        Banner banner = bannerMapper.selectByPrimaryKey(id);
        if (null == banner) {
            throw new CommonException("您查询的轮播图不存在");
        }
        BannerVo bannerVo = new BannerVo();
        BeanUtils.copyProperties(banner, bannerVo);
        return bannerVo;
    }
}
